package com.example.app.pages.main

import android.util.Log
import androidx.activity.compose.BackHandler
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.asPaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.ime
import androidx.compose.foundation.layout.statusBars
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.dp
import com.example.app.barStateViewModel
import com.example.app.components.MySurface.MySurface
import com.example.app.components.MyTip.MyTip
import com.example.app.components.MyTip.MyTipState
import com.example.app.mainViewModel
import com.example.app.myMapSelectedState
import com.example.app.pages.main.home.Home
import com.example.app.pages.main.home.accountList.viewModel.AccountListViewModel
import com.example.app.pages.main.home.addNew.addRecord.viewModel.AddRecordViewModel
import com.example.app.pages.main.home.me.viewModel.AddNewViewModel
import com.example.app.pages.main.home.viewModel.HomeViewModel
import com.example.app.pages.main.login.Login
import com.example.app.pages.main.login.viewModel.LoginViewModel
import com.example.app.routeViewModel
import com.example.app.viewmodel.AddNewChild
import com.example.app.viewmodel.BarStateViewModel
import com.example.app.viewmodel.MainPages
import com.example.app.viewmodel.MainViewModel
import com.example.app.viewmodel.RouteViewModel
import com.google.accompanist.systemuicontroller.rememberSystemUiController
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

var statusBarHeight = 0.dp
var mainTipState = MyTipState()
@Composable
fun Main() {
    var density = LocalDensity.current
    //  var addRecordViewModel: AddRecordViewModel? = null
    var scope = rememberCoroutineScope()
    var curPage = routeViewModel.curPage.collectAsState()
    var statusBarDarkIcon = barStateViewModel.statusBarDarkIcon.collectAsState()
    var navigationBarDarkIcon = barStateViewModel.navigationBarDarkIcon.collectAsState()
    var statusBarColor = barStateViewModel.statusBarColor.collectAsState()
    var navigationBarColor = barStateViewModel.navigationBarColor.collectAsState()
    val systemUiController = rememberSystemUiController()
    var curAddNewChild = routeViewModel.curAddNewChild.collectAsState()
    var userInfo = mainViewModel.userInfo.collectAsState()
    var imePadding = WindowInsets.ime.asPaddingValues()
        .calculateBottomPadding()
    mainViewModel.flowUpdate_imePaddingBottomValue(imePadding)
    var maxImePaddingBottomValue by remember {
        mutableStateOf(0.dp)
    }
    if (imePadding > maxImePaddingBottomValue) {
        maxImePaddingBottomValue = imePadding
        mainViewModel.flowUpdate_imeMaxPaddingBottomValue(maxImePaddingBottomValue)
    }
    if (statusBarHeight < 1.dp) {
        var statusBarHeightPx = WindowInsets.statusBars.getTop(density)
        statusBarHeight = with(density) {
            statusBarHeightPx.toDp()
        }
    }
    //路由
    //全局监听回退按钮，需要全局监听，不然每个componse都会在根componse里面起作用
    BackHandler(
        enabled = true
    ) {
        //关闭地图选择
        if (myMapSelectedState.mapOpenState.value) {
            myMapSelectedState.mapOpenState.value = false
        }
        //关闭地点选择
        else if (myMapSelectedState.showPlaceBottomSheet.value) {
            myMapSelectedState.showPlaceBottomSheet.value = false
        } else {
            routeViewModel.flowUpdate_curAddNewChild(null)
        }
    }
    systemUiController.run {
        setStatusBarColor(
            color = statusBarColor.value,
            darkIcons = statusBarDarkIcon.value
        )
        setNavigationBarColor(
            //设置透明
            color = navigationBarColor.value,
            //图标为黑底 , 设置成黑底 , 背景色才能是白色透明
            darkIcons = navigationBarDarkIcon.value
        )
        //设置顶部和底部 , 这里可以不用设置
        /*setSystemBarsColor(
            color = Color.Transparent,
            darkIcons = true
        )*/
    }

    Box(
        modifier = Modifier.fillMaxSize()
    ) {
        MySurface(
            modifier = Modifier.fillMaxSize(),
        ) {
            when (curPage.value) {
                MainPages.HOME -> {
                    //登录完成后再初始化
                    if (userInfo.value != null) {
                        Home()
                    }
                }

                MainPages.LOGIN -> {
                    Login()
                }

                else -> {
                }
            }
        }
        MyTip(myTipState = mainTipState)
    }
}